package main

import (
	"flag"
	"jkgo/jk/jklog"
	"jkgo/jkbase"
	ws "findService/ss/jkws/ws"
)

type wsArgsInfo struct {
	jkbase.ConfigBaseArgs
	CertFile string
	KeyFile string
}

var wsArgs wsArgsInfo

var (
	cmd   = flag.String("cmd", "run", "run/install/remove service")
	conf  = flag.String("conf", "etc/findyouwant/jkws.json", "conf file")
	debug = flag.Bool("d", false, "debug mode")
)

func start() {
	jklog.L().Infof("iot connect to [%s:%d]\n", wsArgs.ClientAddr, wsArgs.ClientPort)

	err := ws.WSStartTls(wsArgs.Addr, wsArgs.Port, wsArgs.ClientAddr, wsArgs.ClientPort,
		wsArgs.CertFile, wsArgs.KeyFile)
	if err != nil {
		jklog.L().Errorln(err)
	}
}

func main() {

	flag.Parse()

	err := jkbase.GetConfigInfo(*conf, &wsArgs)
	if err != nil {
		jklog.L().Errorf("get config %s failed %v\n", *conf, err)
		return
	}
	jklog.L().InitLog(wsArgs.LogFile)
	if *debug {
		jklog.L().SetLevel(jklog.LEVEL_ALL)
	}
	jklog.L().Infoln("jkws program start...")
	jklog.L().Infoln("conf data ", wsArgs)

	_, err = jkbase.InitProgramRun("jkws", "jk websocket program", start)
	if err != nil {
		jklog.L().Errorln(err)
		return
	}
	jklog.L().Infoln("jkws program exit...")
}
